require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')

// 字体
require('../libs/fonts/iconfont.css')
//轮播
require('../libs/swiper/swiper-bundle.css')
const Swiper = require('../libs/swiper/swiper-bundle')

require('../css/home.less')

//axios请求
const axios = require('axios')

// weui-插件库
require('weui')
const weui = require('weui.js')

//导入util
const util = require('../util/util')




// 文档加载事件
document.addEventListener('DOMContentLoaded',function(){

    /**
     * 0.获取元素
     */
     const rankNum = document.querySelector('#rankNum')//今日排名9
     const punchIn = document.querySelector('#punchIn')//累计打卡
     const clock = document.querySelector('#clock')//今日打卡
     const insigniaNum = document.querySelector('#insigniaNum')//运动徽章
     const motionDate = document.querySelector('#motionDate')//运动数据
     const motionBadge = document.querySelector('#motionBadge')//运动徽章


    /**
     * 0.底部导航
    */
    util.fixBottomNav('home')

    /**
     * 1.轮播图
     */
    
    function setSwiper() {
        var mySwiper = new Swiper('.swiper',{
            loop : true,
            autoplay:{
                delay:3000,
                disableOnInteraction: false,
            },
             // 如果需要分页器
             pagination: {
                el: '.swiper-pagination',
              },
              
        }) 
    }
    setSwiper()

    /**
   * 2.首页数据渲染
   */
  axios.get('http://139.9.177.51:8099/headPageInfo', {
    params: {
      userId: localStorage.getItem('userId')
    }
  }).then(function (res) {
    // console.log(res.data);

    if (res.data.status === 0) {
        rankNum.textContent = res.data.data.rank
        punchIn.textContent = res.data.data.punchIn
        insigniaNum.textContent = res.data.data.insigniaNum

      res.data.data.isPunch === 'true' ? clock.textContent = '已打卡' : clock.textContent = '今日打卡'
    }
  })


  /**
   * 3.今日打卡
   */
   clock.addEventListener('click', function () {
        axios.get('http://139.9.177.51:8099/clockIn', {
          params: {
            userId: localStorage.getItem('userId')
          }
        }).then(function (res) {
            // console.log(res.data);

          if (res.data.status === 0) {
            punchIn.textContent ++
            clock.textContent = '已打卡'
            weui.toast('打卡成功', 1000)
          } else {
            weui.toast('已打卡', 1000)
          };
        })
    })


  /**
   * 4.运动数据，运动徽章跳转页面
   */
  motionDate.addEventListener('click',function(){
     location.href = './myData.html'
  }) 
  motionBadge.addEventListener('click',function(){
    location.href = './myBadge.html'
  }) 



})

   